package com.renzhebiao.lkang.master;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class TokenInterceptor implements HandlerInterceptor {

    // 逻辑执行完毕后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse reponse, Object arg2,
                                Exception arg3) throws Exception {
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
                           ModelAndView arg3) throws Exception {

    }

    // 请求时执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        // TODO Auto-generated method stub
        // 添加跨域CORS
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
        response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");

        System.out.println(request.getRequestURI());
        // 目前没有token，让其通过
        // if (request.getRequestURI().equals("/user/register") || request.getRequestURI().equals("/login")) {
        // return true;
        // } else if (request.getHeader("token") == null) {
        // System.out.println("token 为空");
        // return false;
        //
        // } else {
        // System.out.println(request.getHeader("token"));
        // // 使用验签
        // String token = request.getHeader("token");
        // Claims claims = jwtUtil.parseJWT(token);
        // if (claims.get("userName") != null) {
        // return true;
        // }
        // return false;
        // }
        return true;
    }

}
